﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace SEO.Utility
{
    /// <summary>
    /// 版权所有: 版权所有(C) 2011，Adin Lee
    /// 内容摘要: 操作CONFIG文件，写入功能
    /// 完成日期：2011年4月14日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// </summary>
    public class ConfigHelper
    {
        /// <summary>
        /// 更改APP.CONFIG
        /// </summary>
        /// <param name="strKey">键名</param>
        /// <param name="strValue">键值</param>
        public static void WriteSetting(string strKey, string strValue)
        {
            XmlDocument doc = new XmlDocument();
            string strPath = System.Windows.Forms.Application.ExecutablePath;
            doc.Load(strPath + ".config");
            XmlNode node = doc.SelectSingleNode("//appSettings");
            if (node == null)
                throw new InvalidOperationException("appSettings section not found in config file.");
            try
            {
                XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", strKey));
                if (elem != null)
                {
                    elem.SetAttribute("value", strValue);
                }
                else
                {
                    //elem = doc.CreateElement("add");
                    //elem.SetAttribute("key", key);
                    //elem.SetAttribute("value", value);
                    //node.AppendChild(elem);
                }
                doc.Save(strPath + ".config");
            }
            catch
            {
                throw;
            }
        }
    }
}
